import qs from 'qs'
export default function request(options) {
    /**
     * @param {object} data 传参
     * @param {string} method 请求方法
     * @param {string} url url
     * @param {object} header 请求头
     */
    const {
        data,
        method,
        url,
        header
    } = options
    return new Promise(function (resolve, reject) {
        uni.request({
            url: url,
            method: method || "GET",
            header: header || { 'content-type': "application/json" },
            data:  qs.stringify(data),
            success: (res) => {
                // console.log("网络请求success:" + JSON.stringify(res));
                if (res.data.respCode === '0000') {
                    resolve(res.data)
                } else {
                    uni.showToast({
                        title: res.data.respMsg,
                        icon: 'none',
                        duration: 2000
                    });
                    return
                }
            },
            fail: (e) => {
                // console.log("网络请求fail:" + JSON.stringify(e));
                uni.showToast({
                    title: "网络请求fail",
                    icon: 'none',
                    duration: 2000
                });
                resolve(e.data)
            },
            complete: () => {
                // console.log("网络请求complete");
                resolve();
                return;
            }
        });
    })
}